﻿import GameConfig from "./GameConfig";
import ContentGridRoot from "./ContentGridRoot"
import ScreenPositionUtil from "./ScreenPositionUtil";
import testSceneData from "./testJson";
import testSceneData2 from "./testJson2";

import ContentAreaRoot from "./ContentAreaRoot";


class Main {
	constructor() {
		this.setGridData(testSceneData);
		//根据IDE设置初始化引擎		
		this.onCreateCommand(GameConfig.width, GameConfig.height, 1)
	}

	/**
	 * 设置网格数据
	 * @param {*} e 
	 */
	setGridData(e) {
		GameConfig.gameData = e;
	}

	/**
	 * 创建场景 通过传入屏幕尺寸
	 * @param {*} inW 屏幕宽度
	 * @param {*} inH 屏幕高度
	 * @param {*} inPr 像素比
	 */
	onCreateCommand(inW, inH, inPr) {
		GameConfig.width = inW * inPr;
		GameConfig.height = inH * inPr;
		Laya.Config.isAntialias = true;
		if (window["Laya3D"]) Laya3D.init(GameConfig.width, GameConfig.height);
		else Laya.init(GameConfig.width, GameConfig.height, Laya["WebGL"]);
		Laya["Physics"] && Laya["Physics"].enable();
		Laya["DebugPanel"] && Laya["DebugPanel"].enable();
		Laya.stage.scaleMode = GameConfig.scaleMode;
		Laya.stage.screenMode = GameConfig.screenMode;
		Laya.stage.alignV = GameConfig.alignV;
		Laya.stage.alignH = GameConfig.alignH;
		//兼容微信不支持加载scene后缀场景
		Laya.URL.exportSceneToJson = GameConfig.exportSceneToJson;

		//打开调试面板（通过IDE设置调试模式，或者url地址增加debug=true参数，均可打开调试面板）
		if (GameConfig.debug || Laya.Utils.getQueryString("debug") == "true") Laya.enableDebugPanel();
		if (GameConfig.physicsDebug && Laya["PhysicsDebugDraw"]) Laya["PhysicsDebugDraw"].enable();
		if (GameConfig.stat) Laya.Stat.show();
		Laya.alertGlobalError(true);

		//激活资源版本控制，version.json由IDE发布功能自动生成，如果没有也不影响后续流程
		Laya.ResourceVersion.enable("version.json", Laya.Handler.create(this, this.onVersionLoaded), Laya.ResourceVersion.FILENAME_VERSION);
		
		ScreenPositionUtil.setStageWidthAndHeight(Laya.stage.width, Laya.stage.height);
		this.initScene();
	}

	/**
	 * 初始化场景
	 */
	initScene() {

		this.ContentGridRootView = new ContentGridRoot(this);
		Laya.stage.addChild(this.ContentGridRootView);

		this.ContentAreaRoot = new ContentAreaRoot(this);
		this.ContentAreaRoot.initByContentAreaDatas(GameConfig.gameData.contentAreaList);
		Laya.stage.addChild(this.ContentAreaRoot);

		let switchTexture = function () {
			if (this.ContentAreaRoot != null) {
				this.ContentAreaRoot.tryAddPicInArea(testSceneData.candidateImagesList[0]);
			}
		}

		let testBtn = new Laya.Sprite;
		testBtn.pos(50, 50);
		testBtn.loadImage("https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/EICO_JORDAN/scene/picture-puzzle/shoes/shoe_0.png");
		testBtn.size(100, 100);
		testBtn.on(Laya.Event.CLICK, this, switchTexture);
		Laya.stage.addChild(testBtn);



		let addText = function () {
			this.tryAddTextToScene(["我是一行fdsafdasfasdffdsaf文字", "我是er行fdsafdasfasdffdsaf文字"])
		}

		let testBtnText = new Laya.Sprite;
		testBtnText.pos(150, 50);
		testBtnText.loadImage("https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/EICO_JORDAN/scene/picture-puzzle/shoes/shoe_0.png");
		testBtnText.size(100, 100);
		testBtnText.on(Laya.Event.CLICK, this, addText);
		Laya.stage.addChild(testBtnText);


		let showBorder = function () {
			this.isShowGlobalGrid(false);
		}

		let test2Btn = new Laya.Sprite;
		test2Btn.pos(50, 100);
		test2Btn.loadImage("https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/EICO_JORDAN/scene/picture-puzzle/shoes/shoe_0.png");
		test2Btn.size(100, 100);
		test2Btn.on(Laya.Event.CLICK, this, showBorder);
		Laya.stage.addChild(test2Btn);

		let show2Border = function () {
			// this.isShowGlobalGrid(true);
			// this.clearContents();
			this.clearAndResetGrid(testSceneData2);
		}

		let test3Btn = new Laya.Sprite;
		test3Btn.pos(150, 100);
		test3Btn.loadImage("https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/EICO_JORDAN/scene/picture-puzzle/shoes/shoe_0.png");
		test3Btn.size(100, 100);
		test3Btn.on(Laya.Event.CLICK, this, show2Border);
		Laya.stage.addChild(test3Btn);
	}

	tryAddPicToScene(inData, inAreaId) {
		let re = false;
		if (this.ContentAreaRoot != null) {
			re = this.ContentAreaRoot.tryAddPicInArea(inData);
		}
		return re;
	}

	tryAddTextToScene(inStrs) {
		let re = false;
		if (this.ContentAreaRoot != null) {
			re = this.ContentAreaRoot.tryAddTextInArea(inStrs);
		}
		return re;
	}

	isShowGlobalGrid(e) {
		this.ContentAreaRoot.setBorderAndTipVisible(e);
		this.ContentGridRootView.updateGridVisible(e);
	}

	clearContents() {
		this.tryAddTextToScene(["",""]);
		this.ContentAreaRoot.clearContents();
	}

	clearAndResetGrid(inData) {
		Laya.stage.removeChildren(0,1000);
		GameConfig.gameData = inData;
		this.initScene();
	}

	onVersionLoaded() {
		//激活大小图映射，加载小图的时候，如果发现小图在大图合集里面，则优先加载大图合集，而不是小图
		Laya.AtlasInfoManager.enable("fileconfig.json", Laya.Handler.create(this, this.onConfigLoaded));
	}

	onConfigLoaded() {
		//加载IDE指定的场景
		GameConfig.startScene && Laya.Scene.open(GameConfig.startScene);
	}
}
//激活启动类
new Main();
